package cn.aran.controller;

import cn.aran.domain.Product;
import cn.aran.feign.StoreFeignClient;
import cn.aran.service.IProductService;
import cn.aran.utils.AjaxResult;
import cn.aran.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Autowired
    private StoreFeignClient storeFeignClient;

    @PostMapping("/add")
    public AjaxResult addProduct(@RequestBody ProductVo productvo){
        try {
            Product product = new Product();
            BeanUtils.copyProperties(productvo, product);
            productService.add(product);
            productvo.setProductId(product.getId());
            System.out.println(productvo);
            // 库存表
            storeFeignClient.addStore(productvo);
            return AjaxResult.me();
        } catch (BeansException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("添加失败!");
        }
    }
}
